package com.nd.android.u.ui.beanStarApp;

import ims.IMSdkEntry;
import ims.utils.CommUtil;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;

import com.common.StarAppConfig;
import com.common.allCommon.StarAppHeadImageLoader;
import com.common.android.utils.audio.AudioPlayer;
import com.common.android.utils.smiley.Smiley;
import com.common.android.utils.smiley.Smileyhelper;
import com.nd.android.u.allCommonUtils.PackageUtils;
import com.nd.android.u.chatInterfaceImpl.ChatInterfaceImpl;
import com.nd.android.u.commonWidget.NotificationMsg;
import com.nd.android.u.controller.ChatConst;
import com.nd.android.u.controller.ChatConst.KEY;
import com.nd.android.u.controller.ChatGlobalVariable;
import com.nd.android.u.controller.bean.message.BaseDisplayMessage_Group;
import com.nd.android.u.ui.ChatEntry;
import com.nd.android.u.ui.R;
import com.product.android.business.ApplicationVariable;
import com.product.android.commonInterface.chat.ChatGroup;

/**
 * 群消息展示类，有些UI相关的操作要在这里进行
 * <br>Created 2014-10-30 上午11:05:54
 * @version  
 * @author   cb		
 * @see 	 
 */
public class DisplayMessage_Group extends BaseDisplayMessage_Group{

	/**
	 * DisplayMessage_Group.构造方法
	 * <br>Created 2014-10-30 上午11:07:06
	 * @param gid
	 * @param type
	 */
	public DisplayMessage_Group(String gid, int type) {
		super(gid, type);
	}

	@Override
    public void showNotify() {
	
    	if (ChatGlobalVariable.getInstance().noNeedNotify(this)){
            //不需要显示通知
            return;     
        }
        Context context = IMSdkEntry.INSTANCE.context;
        // 设置点击后跳转的目标
        Intent notifyIntent = new Intent();
        // 在通知栏上面显示“xxx发来一条新消息”
        String flashText = "";
        long gid = CommUtil.parseLong(groupId);
        if (groupType==ChatGroup.getUntidGroupType()){
        	//单位群
        	gid=ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getUnitid();
        }
        String groupName = ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getGroupName(groupType, gid);
		/** 无效的群名 */
		if (groupName==null||groupName.equals("群null") || groupName.equals("null")
				|| groupName.equals("")) {// 过滤无效的群名
			return;
		}
		
        if (groupType == ChatGroup.getNormalGroupType()) {
            flashText = context.getString(R.string.notify_group, groupName);
        } else if (groupType == ChatGroup.getDiscussionGroupType()) {
            flashText = context.getString(R.string.notify_discussion, groupName);
        } else if (groupType == ChatGroup.getDepartGroupType()) {
            flashText = context.getString(R.string.notify_group, groupName);
        } else if (groupType == ChatGroup.getUntidGroupType()) {
            /** 群公告 */
            flashText = context.getString(R.string.notify_broadcast);
        }
//         显示通知的具体内容（为消息的文本）
        String tipText = Smileyhelper.getInstance().showSmileyByTip(displayContent, Smiley.TYPE_CHAT);
        if(TextUtils.isEmpty(tipText)){
            return;
        }
        // 设置剩余多少条未读消息
        String title = getUnreadMessage();
        if(TextUtils.isEmpty(title)){
            return;
        }
        notifyIntent.setClass(context, getDisplayClass());
        Bundle bundle = new Bundle();
        bundle.putLong(ChatConst.KEY.GID, gid);
        bundle.putLong(ChatConst.KEY.GENERAL_ID, gid);
        bundle.putInt(ChatConst.KEY.GROUPTYPE, groupType);
        bundle.putInt(ChatConst.KEY.MESSAGE_TYPE, messageType);
        bundle.putString(KEY.NAME, groupName);
        notifyIntent.putExtras(bundle);
        
        // 生成通知
        Notification notification = NotificationMsg.getInstance().obtainNotify(flashText, title, tipText,
                notifyIntent);
        // 显示通知
        NotificationMsg.getInstance().showNotify(notification, this, true, bundle);
    }
	@Override
	public String getThumbnailUrl() {
	    return url;
	}

	@Override
	public void clickPortrait(boolean isMyPortrait, Context context) {
		if (isMyPortrait) {
			// ChatEntry.INSTANCE.chatCallOtherModel_UI.gotoTweetProfileActivity(context,
			// ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getOapUid());
			ChatEntry.INSTANCE.chatCallOtherModel_UI
					.gotoPersonalDynamicActivity(context,
							ApplicationVariable.INSTANCE.getUapUid());
		} else {
			// ChatEntry.INSTANCE.chatCallOtherModel_UI.gotoTweetProfileActivity(context,
			// uidFrom);
			ChatEntry.INSTANCE.chatCallOtherModel_UI
					.gotoPersonalDynamicActivity(context, uidFrom);
		}
	}
    

    @Override
	public void displayPortrait(ImageView img, boolean isMyPortrait) {
		if (isMyPortrait) {
			// StarAppHeadImageLoader.displayCircleImage(ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getOapUid(),
			// StarAppHeadImageLoader.IMAGE_TYPE_HEAD_100, img, null);
			StarAppHeadImageLoader.displayCircleImageByUid(
					// FIDS 群聊头像改造
					ApplicationVariable.INSTANCE.applicationContext,
					ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getOapUid(),
					StarAppHeadImageLoader.IMAGE_TYPE_HEAD_100, img, null, -1,
					-1);
		} else {
			// StarAppHeadImageLoader.displayCircleImage(uidFrom,
			// StarAppHeadImageLoader.IMAGE_TYPE_HEAD_100, img, null);
			StarAppHeadImageLoader.displayCircleImageByUid(
					ApplicationVariable.INSTANCE.applicationContext,
					getFriendId(), StarAppHeadImageLoader.IMAGE_TYPE_HEAD_100,
					img, null, -1, -1);
		}
	}

//    @Override
//    public int getDuration() {
//        int dur=super.getDuration();
//        if (dur<=0){
//            return AudioPlayer.getAudioDuration(getPath());
//        }else{
//            return dur;
//        }
//    }
}
